package com.zh.view.component


import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.zh.view.FileDataItem
import com.zh.view.formatStatus
import com.zh.view.getStatusColor


@Composable
fun FileTaskTable(
    data: SnapshotStateList<FileDataItem>,
    modifier: Modifier = Modifier,
    actions: List<RowAction<FileDataItem>>
) {
    Column(modifier.fillMaxSize()) {
        // 表头
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .padding(12.dp),
            verticalAlignment = Alignment.CenterVertically
        ) {
            Text("文件名", modifier = Modifier.weight(1f), color = MaterialTheme.colorScheme.onSurfaceVariant)
            Text("进度", modifier = Modifier.weight(2f), color = MaterialTheme.colorScheme.onSurfaceVariant)
            Text("状态", modifier = Modifier.weight(1f), color = MaterialTheme.colorScheme.onSurfaceVariant)
            Text(" ", modifier = Modifier.weight(1f), color = MaterialTheme.colorScheme.onSurfaceVariant)
            Box(modifier = Modifier.weight(0.5f)) // 操作列占位
        }

        // 内容列表
        LazyColumn(modifier = Modifier.weight(1f)) {
            items(data) { item ->
                TableRow(
                    item = item,
                    actions = actions
                )
            }
        }

    }
}


@Composable
private fun TableRow(item: FileDataItem, actions: List<RowAction<FileDataItem>> /*,  onDelete: () -> Unit*/) {
    val progressValue by remember { item.progressValue }
    val status by remember { item.status }

    Card(
        modifier = Modifier
            .fillMaxWidth()
            .padding(vertical = 4.dp, horizontal = 8.dp)
    ) {
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .padding(12.dp),
            verticalAlignment = Alignment.CenterVertically
        ) {
            // 名称列
            Text(
                text = item.fileName,
                modifier = Modifier.weight(1f)
            )

            // 进度条列
            Column(modifier = Modifier.weight(2f)) {
                LinearProgressIndicator(
                    progress = { progressValue },
                )
                Text(
                    text = "${(progressValue * 100).toInt()}%",
                    fontSize = 12.sp,
                    modifier = Modifier.padding(top = 4.dp)
                )
            }

            // 状态列 - 新增
            Box(
                modifier = Modifier.weight(1f),
                contentAlignment = Alignment.CenterStart
            ) {
                Text(
                    text = formatStatus(status),
                    color = getStatusColor(status),
                    fontWeight = FontWeight.Medium
                )
            }

            // 操作列
            Box(
                modifier = Modifier.weight(1f),
                contentAlignment = Alignment.CenterEnd
            ) {
                Row {
                    actions.forEach { action ->
                        Icon(
                            imageVector = action.icon,
                            contentDescription = action.contentDescription,
                            tint = action.tint,
                            modifier = Modifier
                                .clickable { action.onClick(item) }
                                .padding(8.dp)
                        )
                    }
                }
            }
        }
    }
}

data class RowAction<T>(
    val icon: ImageVector,
    val contentDescription: String,
    val tint: Color,
    val onClick: (T) -> Unit
)